﻿using System.Threading;

namespace CoCoonCoreWM6
{
    public class CoCo
    {
        private VDG Vdg { get; set; }

        private CPU Cpu { get; set; }

        private bool Paused { get; set; }
        
        public CoCo(int ramSize)
        {
            //initialize RAM memory
            RAM.Initialize(ramSize);
            RAM.TestProgram();

            //initialize ROM

            //initialize VDG
            Vdg = new VDG();

            //initialize CPU
            Cpu = new CPU();


            //set CPU time slice to 1 ms
            //Util.timeBeginPeriod(1);
        }

        public void Run(int periodInMs)
        {
            int maxCycles = ((1/Cpu.ClockingSpeed)*1000)/periodInMs;
            int cycleCount = 0;
            while (cycleCount < maxCycles)
            {
                cycleCount = cycleCount + Cpu.ExecuteCurrentInstruction();
            }
        }

        public Pixel[] VideoOut()
        {
            return Vdg.GetFrame();
        }

        public void KeyboardInput(int? keyValue)
        {
            if (keyValue != null)
            {
                //set input
            }
            else
            {
                //clear input
            }
        }

        public void TearDown()
        {
            //Util.timeEndPeriod(1);  
        }
    }
}
